Unit Items;

{$mode objfpc}{$H+}

Interface

Uses
  Crt,
  Initializer,
  ItemProperties,
  MenuDraw,
  DefaultClasses,
  EnA;


Var
   UsedItem : Boolean;
   ChosenID : Integer;


Procedure GeneralShop;
Procedure ItemAction;
Procedure Battle_ItemList;
Procedure ItemDisplay;

Implementation

Procedure GeneralShop;
Var
   CharSelection : Char;
   Selection, Amount, Count, ItemCount, MenuMod : Integer;

Begin
  MenuMod := 0;

  Repeat
    Count := 1;
    Clrscr;
    Writeln('Welcome to the shop!');
    Writeln('Press the appropriate number to the item you wish to purchase');
    Writeln(Gold);
    Writeln;
    For ItemCount := 1 to 5 do
      Writeln(ItemCount,': ',ItemIDName[ItemCount + MenuMod],' - ',ItemPrice[ItemCount + MenuMod],' Gold');

    Writeln;
    Writeln;
    If MenuMod >= 5 then
      Writeln('7.  Previous 5 items');
    Writeln('8.  Next 5 items');
    Writeln('9.  Quit');
    Writeln;

    CharSelection := ReadKey;

    If (CharSelection = #55) and (MenuMod >= 4) then
      Begin
        MenuMod := MenuMod - 5;
        Continue;
      end;

    If CharSelection = #56 then
      Begin
        MenuMod := MenuMod + 5;
        Continue;
      end;

    If CharSelection = #57 then
      Break //Quits loop early by the user's demand.
        else
          If (CharSelection <> #57) and ((CharSelection <= #47) and (CharSelection > #54)) then
            Repeat
              Writeln('Enter the number next to the item you want');
              CharSelection := ReadKey;
            Until (CharSelection > #48) and (CharSelection < #56);
          If (CharSelection > #48) and (CharSelection < #56) then
            Selection := Integer(CharSelection) - 48;
    If (Selection >= 1) and (Selection <= 5) and (Gold >= ItemPrice[Selection + MenuMod]) then
      Repeat
        Writeln('How many ',ItemIDName[Selection + MenuMod],' would you like?');
        Readln(Amount);
      Until Amount >= 0
      else begin
        Writeln('You don''t have enough gold');
        Attention_noclear;
        Continue;
      end;
    For Count := 1 to 20 do
      Begin
        If DItem[Count].ItemAmount < 1 then
          Begin
            DItem[Count].ItemAmount := Amount;
            DItem[Count].Name := ItemIDName[Selection + MenuMod];
            DItem[Count].ItemID := ItemIDNo[Selection + MenuMod];
            Break;
          end;

        If DItem[Count].Name = ItemIDName[Selection + MenuMod] then
          Begin
            DItem[Count].ItemAmount := DItem[Count].ItemAmount + 1;
            Break;
          end;

      end;
    Gold := Gold - (ItemPrice[Selection + MenuMod] * Amount);

    Until CharSelection = #57;

end;

Procedure ItemAction;
Begin
  Case ChosenID of
    1 : ItemLesserPotion(Health);
    2 : ItemManaPotion(Mana);
    3 : ItemPotion(Health);
    4 : ItemGreaterPotion(Health);
    5 : ItemAntidote(Ailment1, Ailment2);
    6 : ItemProteinShake(DamageBoost);
    7 : ItemDiazepam(DamageBoost);
    8 : ItemTonic(DamageBoost);
    9 : ItemXPBOOST(XpBoost);
    //10 : ItemGodsTear;
  end;
end;

Procedure Battle_ItemList;
Var
   Count1 : Integer;
   Slot : Array [1..5] of Integer;
   BattleMenu : Char;
   MenuMod : Integer;

Begin
  MenuMod := 0;
Repeat
  Count1 := 1;
  Writeln('Items: ');

  Repeat
    If DItem[Count1+MenuMod].ItemAmount >= 1 then
      Begin
        Writeln(Count1,': ',DItem[Count1+MenuMod].Name,'(',DItem[Count1+MenuMod].ItemAmount,')');
        Slot[Count1] := DItem[Count1+MenuMod].ItemID;
        Count1 := Count1 + 1;
        Continue
      end else
        break;
  Until Count1 = 6;

  Writeln;
  If MenuMod >= 5 then
    Writeln('7: Previous 5 items');
  If Count1 = 6 then
    Writeln('8: Next 5 items');

  BattleMenu := ReadKey;
  Case BattleMenu of
    #49 : ChosenID := Slot[1];
    #50 : ChosenID := Slot[2];
    #51 : ChosenID := Slot[3];
    #52 : ChosenID := Slot[4];
    #53 : ChosenID := Slot[5];
    #55 : Begin
            If MenuMod >= 5 then
                MenuMod := MenuMod - 5;
          Continue;
          end;
    #56 : Begin
            MenuMod := MenuMod + 5;
            Continue;
          end;

  end;

  ItemAction;
  UsedItem := True;

until UsedItem;
end;

Procedure ItemDisplay;
Var
   Count1 : Integer;
Begin
  Count1 := 1;

  Writeln('Items: ');
  Repeat
    If DItem[Count1].ItemAmount >= 1 then
      Begin
        Writeln(Count1,': ',DItem[Count1].Name,'(',DItem[Count1].ItemAmount,')');
        Count1 := Count1 + 1;
        Continue
      end else
        break;
  Until Count1 = 99;
  Attention;
end;

end.

